﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.Serialization;
using ChinaAzureStorageRedirector.Properties;

namespace ChinaAzureStorageRedirector
{
    public partial class Rules : Form
    {
        List<String> allRules = new List<String>();
        public Rules()
        {
            InitializeComponent();
            clbRules.FormattingEnabled = false;
            XmlSerializer s = new XmlSerializer(typeof(List<Rule>));
           
            allRules = Helper.AllRules;

            foreach (var item in allRules)
            {
                clbRules.Items.Add(item, false);
            }

          
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string strKeyword = this.txtKeyword.Text.Trim();
            if (string.IsNullOrEmpty(strKeyword))
            {
                this.txtKeyword.Focus();
                MessageBox.Show("Keyword can not be empty");
                return;
            }

           
            allRules.Add(strKeyword);
            clbRules.Items.Add(strKeyword, false);
            this.txtKeyword.Text = "";

        }

        private void Rules_FormClosing(object sender, FormClosingEventArgs e)
        {
            XmlSerializer s = new XmlSerializer(typeof(List<String>));
            using (TextWriter WriteFileStream = new StreamWriter(Helper.RuleFilePath))
            {
                s.Serialize(WriteFileStream, allRules);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            while (clbRules.CheckedItems.Count > 0)
            {
                allRules.Remove((String)clbRules.CheckedItems[0]);
                clbRules.Items.Remove(clbRules.CheckedItems[0]);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
